<?php

namespace app\admin\controller;

use think\Db;

class JobDuty extends Base
{

    /**
     * 添加职责
     */
    public function addDuty()
    {
        $data = [];
        input('name') ? $data['name'] = input('name') : $this->err('缺少职责');
        input('jobId') ? $data['jobId'] = input('jobId') : $this->err('缺少岗位编码');
        input('sort') != null ? $data['sort'] = input('sort') : true;

        $data['createTime'] = createTime();

        $res = model('job_duty')->editData($data);
        $res ? $this->suc($res) : $this->err('添加职责失败');
    }

    /**
     * 修改职责
     */
    public function editDuty()
    {
        $data = [];
        input('id') ? $data['id'] = input('id') : $this->err('缺少职责主键');
        input('name') ? $data['name'] = input('name') : $this->err('缺少职责');
        input('jobId') ? $data['jobId'] = input('jobId') : $this->err('缺少岗位编码');
        input('sort') != null ? $data['sort'] = input('sort') : true;

        $res = model('job_duty')->editData($data, $data['id']);
        $res ? $this->suc($res) : $this->err('修改职责失败');
    }

    /**
     * 删除职责
     */
    public function delDuty()
    {
        $where = [];
        input('id') ? $where['id'] = input('id') : $this->err('缺少职责主键');

        $res = model('job_duty')->where($where)->delete();
        $res ? $this->suc($res) : $this->err('删除职责失败');
    }

    /**
     * 职责列表
     */
    public function getDutyList()
    {
        $where = [];
        input('name') ? $where['jd.name'] = ['like', '%' . input('name') . '%'] : true;
        input('jobId') ? $where['jd.jobId'] = input('jobId') : true;

        $res = model('job_duty')
            ->alias('jd')
            ->field('jd.id,jd.createTime, jd.name as dutyName,jd.sort,j.name as jobName')
            ->join('job j', 'j.id=jd.jobId', 'left')
            ->where($where)
            ->order('jd.id desc')
            ->paginate(10)
            ->toArray();
        $res ? $this->suc($res) : $this->err('查询职责失败');
    }

    /**
     * 查询职责详情
     */
    public function getDutyDetail()
    {
        $where = [];
        input('id') ? $where['jd.id'] = input('id') : $this->err('缺少职责主键');

        $res = model('job_duty')
            ->alias('jd')
            ->field('jd.id,jd.createTime, jd.name as dutyName,j.name as jobName, jd.jobId, jd.sort')
            ->join('job j', 'j.id=jd.jobId', 'left')
            ->where($where)
            ->find();
        $res ? $this->suc($res) : $this->err('查询失败');
    }

    /**
     * 根据岗位查询职责
     */
    public function getDutyListByJob()
    {
        $where = [];
        input('jobId') ? $where['j.id']=input('jobId'): $this->err('缺少岗位主键');
        $res = model('job_duty')
            ->alias('jd')
            ->field('jd.id,jd.name as dutyName, jd.sort, j.name as jobName')
            ->join('job j', 'j.id=jd.jobId', 'left')
            ->where($where)
            ->order('jd.id desc')
            ->paginate(10)
            ->toArray();
        $res ? $this->suc($res) : $this->err('获取失败');
    }

}
